package cn.tedu.file;

import java.io.*;

/*本类用于测试字符输出流*/
public class TestWriter {
    public static void main(String[] args) {
       method();//普通
      // method2();//高效
    }
    private static void method2() {
        Writer out = null;
        try {
            //1.创建流
             out = new BufferedWriter(new FileWriter(
                    "D:\\ready\\2.txt",true));
            //2.输出流
            out.write(97);
            out.write("hello");
            out.write("io");
            //刷数据，防止数据丢失
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
        }
        //3.关流
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void method() {
        Writer out = null;
        try {
            out = new FileWriter("D:\\ready\\2.txt",true);
            out.write(97+"\t");
            out.write("hello\n");
            out.write("io");
            out.flush();
        } catch (IOException e) {//用来匹配并捕获异常
            e.printStackTrace();
        }finally {//本结构中一定会执行到的代码块，必要操作比如关流就可以放倒这里
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
